package sshj;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Timer;
import java.util.TimerTask;

import com.jcraft.jsch.SftpProgressMonitor;
import io.SshBean;
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.sftp.SFTPClient;
import net.schmizz.sshj.transport.verification.PromiscuousVerifier;

public class TransferMonitor extends TimerTask {

//    private long progressInterval = 5 * 1000; // 默认间隔时间为5秒

    private boolean isEnd = false; // 记录传输是否结束

    private long transfered; // 记录已传输的数据总大小

    private long fileSize; // 记录文件总大小

    private SshBean sshBean;

    private String destPath = "";

    private SSHClient sshClient = new SSHClient();

    private SFTPClient sftpClient = null;

    public TransferMonitor(long fileSize, SshBean sshBean,String destPath) {
        this.fileSize = fileSize;
        this.sshBean = sshBean;
        this.destPath = destPath;
        start(sshBean);
    }

    @Override
    public void run() {
        if (!isEnd()) { // 判断传输是否已结束
            System.out.println("Transfering is in progress.");
            long transfered = getTransfered();
            if (transfered != fileSize) { // 判断当前已传输数据大小是否等于文件总大小
                System.out.println("Current transfered: " + transfered + " bytes");
                sendProgressMessage(transfered);
            } else {
                System.out.println("Sending progress message: 100%");
                System.out.println("File transfering is done.");
                setEnd(true); // 如果当前已传输数据大小等于文件总大小，说明已完成，设置end
            }
        } else {
            stop();
            System.out.println("Transfering done. Cancel timer.");
            return;
        }
    }
    private void start(SshBean sshBean){
        try {
            sshClient.addHostKeyVerifier(new PromiscuousVerifier());
            sshClient.connect(sshBean.getIp());
            sshClient.authPassword(sshBean.getUsername(), sshBean.getPassword());
            sftpClient = sshClient.newSFTPClient();
        } catch (IOException e){
            System.out.println(e);
            System.out.println("monitor connect ssh fail");
        }
    }

    private void stop(){
        if (sftpClient != null){
            try {
                sftpClient.close();
            } catch (IOException e){
                System.out.println("upload file success, close sftpClient fail");
            }
        }
        if (sshClient != null){
            try {
                sshClient.disconnect();
            } catch (IOException e){
                System.out.println("upload file success, close ssh fail");
            }
        }
        cancel();
    }

    /**
     * 打印progress信息
     * @param transfered
     */
    private void sendProgressMessage(long transfered) {
        if (fileSize != 0) {
            double d = ((double)transfered * 100)/(double)fileSize;
            DecimalFormat df = new DecimalFormat( "#.##");
            System.out.println("Sending progress message: " + df.format(d) + "%");
        } else {
            System.out.println("Sending progress message: " + transfered);
        }
    }


    private synchronized long getTransfered() {
        try {
            transfered = sftpClient.size(destPath);
        } catch (IOException e){
            System.out.println(e);
            System.out.println("get file size error");
        }

        return transfered;
    }

    private synchronized void setEnd(boolean isEnd) {
        this.isEnd = isEnd;
    }

    private synchronized boolean isEnd() {
        return isEnd;
    }
}